import Vue from "vue";
import VueRouter from "vue-router";
import store from "@/store";
import Login from "@/views/Login";
import Layout from "@/views/Layout";
import NProgress from "nprogress";
import "nprogress/nprogress.css";

Vue.use(VueRouter);

// 路由
let constantRoutes = [
  {
    path: "/login",
    name: "login",
    component: Login,
    meta: { isTabs: false, isSide: false, moduleName: "main", title: "登录" },
  },
];
const createRouter = () => {
  return new VueRouter({
    mode: "history",
    base: window.__POWERED_BY_QIANKUN__ ? "/NewFrontTemplate/" : "/",
    routes: constantRoutes,
    isAddAsyncMenuData: false,
  });
};

// 处理重复点击同一个路由报错的问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

const router = createRouter();
/**
 * 重置注册的路由导航map
 * 主要是为了通过addRoutes方法动态注入新路由时，避免重复注册相同name路由
 */
const resetRouter = () => {
  const newRouter = createRouter();
  router && (router.matcher = newRouter.matcher);
};
const whiteList = ["/login"];
//路由守卫
router.beforeEach((to, from, next) => {
  NProgress.start();
  const isLogged = !!sessionStorage.getItem("authorization_token");
  if (to.path === "/login" && isLogged) {
    next("/");
  } else if (isLogged || whiteList.indexOf(to.path) !== -1) {
    // 菜单当前选中及页面持久
    if (to.path !== "/login") {
      store.commit("permission/UPDATE_CURRENT_MODULE_NAME", to.meta.moduleName);
      store.commit("permission/UPDATE_CURRENT_PAGE", to.path);
      store.commit("permission/UPDATE_CURRENT_TABNAME", to.meta.title);
    }
    if (!router.options.isAddAsyncMenuData) {
      store.dispatch("permission/generateRoutes").then((accessRoutes) => {
        // 根据用户权限生成可访问的路由表
        let mainRoutes = [];
        for (let i = 0, length = accessRoutes.length; i < length; i += 1) {
          const element = accessRoutes[i];
          // if (element.meta.moduleName === "Main") {
          //   mainRoutes.push(element);
          // } else {
          //   router.addRoute(element);
          // }
          router.addRoute(element);
        }
        router.addRoute({
          path: "/",
          name: "Layout",
          component: Layout,
          redirect: process.env.VUE_APP_DEFAULT_APP, // 默认加载的路由
          children: mainRoutes,
        });
        router.options.isAddAsyncMenuData = true;
        next({ ...to, replace: true }); // hack方法 确保addRoutes已完成
      });
    } else {
      next();
    }
  } else {
    next("/login");
  }
});
router.afterEach(() => {
  NProgress.done();
});

export { constantRoutes, resetRouter };

export default router;
